
var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);

router.post("/", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body,
      unit_number = param.unit_number;
  param.appId = req.cookies.appId;

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/query_all',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/create", function(req, res, next) {
  var token = req.cookies.access_token;
  var param = req.body,
      unit_number = param.unit_number,
      sys_user = param.sys_user,
      unit_desc = param.unit_desc,
      property_id = param.property_id
  if (!unit_number || !property_id) {
    return res.status(500).json({
      success: false,
      errMsg: '请输入完整信息'
    })
  }
  var data = {
    unit_number: unit_number,
    property_id: property_id,
    unit_desc: unit_desc
  }
  if (sys_user) {
    data.sys_user_id = sys_user.id;
  }
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/create',
    body: data,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/update", function(req, res, next) {
  var token = req.cookies.access_token;
  var param = req.body,
      id = param.id,
      unit_number = param.unit_number,
      unit_desc = param.unit_desc,
      sys_user = param.sys_user||null;

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/update',
    body: {
      unit_number: unit_number,
      unit_desc: unit_desc,
      sys_user_id: sys_user?sys_user.id:null,
      id: id
    },
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/delete", function(req, res, next) {
  var token = req.cookies.access_token;
  var param = req.body,
      id = param.id

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/delete',
    body: {
      id: id
    },
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/query", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/query',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/queryUserBindByUnit", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/units/queryUserBindByUnit',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 查询物业管家
router.post("/querySysUser", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/sysUserUnit/querySysUser',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 提交物业管家
router.post("/SubmitSysUser", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/sysUserUnit/bind',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


module.exports = router
